package com.wocai.jrt.orgadmin.riskEvaluation.action;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.GenericAction;
import com.vteba.web.action.JsonBean;
import com.wocai.jrt.orgadmin.investor.bean.GroupBean;
import com.wocai.jrt.orgadmin.investor.service.spi.InvestorService;
import com.wocai.jrt.orgadmin.riskEvaluation.model.Answer;
import com.wocai.jrt.orgadmin.riskEvaluation.model.Options;
import com.wocai.jrt.orgadmin.riskEvaluation.model.Paper;
import com.wocai.jrt.orgadmin.riskEvaluation.model.Question;
import com.wocai.jrt.orgadmin.riskEvaluation.service.spi.AnswerService;
import com.wocai.jrt.orgadmin.riskEvaluation.service.spi.OptionsService;
import com.wocai.jrt.orgadmin.riskEvaluation.service.spi.PaperService;
import com.wocai.jrt.orgadmin.riskEvaluation.service.spi.QuestionService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 测评试卷控制器
 * @author zhangyz
 * @date 2015-10-28 16:03:03
 */
@Controller
@RequestMapping("/paper")
public class PaperAction extends GenericAction<Paper> {
	private static final Logger LOGGER = LoggerFactory.getLogger(PaperAction.class);
	
	@Inject
	private PaperService paperServiceImpl;
	@Inject
	private QuestionService questionServiceImpl;
	@Inject 
	private InvestorService investorServiceImpl;
	@Inject
	private AnswerService answerServiceImpl;
	@Inject
	private OptionsService optionsServiceImpl;
	
	/**
	 *跳转到问卷列表页面
	 */
	@RequestMapping("/list")
	public String list(Paper model, Map<String, Object> maps) {
		List<Paper> list = null;
		try {
			list = paperServiceImpl.pagedList(model);
			maps.put("paperList", list);
		} catch (Exception e) {
			LOGGER.error("get paper list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "riskEvaluation/paperList";
	}
	
	/**
	 *跳转到新增问卷页面
	 */
	@RequestMapping("/add")
	public String add(Paper model, Map<String, Object> maps) {
		List<Paper> list = null;
		try {
			list = paperServiceImpl.pagedList(model);
			maps.put("paperList", list);
		} catch (Exception e) {
			LOGGER.error("get paper list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "riskEvaluation/addPaper";
	}
	
	/**
	 * 跳转到题库管理页面
	 */
	@RequestMapping("/manager")
	public String manager(Question model, Map<String, Object> maps) {
		List<Question> list = null;
		try {
			model.setOrgId(UserUtils.getOrgId());
			list = questionServiceImpl.pagedList(model);
			maps.put("list", list);
		} catch (Exception e) {
			LOGGER.error("get paper list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "riskEvaluation/manager";
	}
    
	/**
	 * 跳转到测评结果管理页面
	 */
	@RequestMapping("/evaluList")
	public String evaluList(Question model, Map<String, Object> maps) {
		try {
			// 获取机构下所有组别列表
			GroupBean groupBean = new GroupBean();
			groupBean.setOrgId(UserUtils.getOrgId());
			List<GroupBean> groupList = investorServiceImpl.getGroupList(groupBean);
			maps.put("groupList", groupList);
		} catch (Exception e) {
			LOGGER.error("get evaluList list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "riskEvaluation/evaluRes";
	}
	
	/**
	 * 跳转到测评结果管理页面
	 */
	@RequestMapping("/addPaper")
	public String addPaper(Question model, Map<String, Object> maps) {
		try {
			// 获取机构下所有组别列表
			GroupBean groupBean = new GroupBean();
			groupBean.setOrgId(UserUtils.getOrgId());
			List<GroupBean> groupList = investorServiceImpl.getGroupList(groupBean);
			maps.put("groupList", groupList);
		} catch (Exception e) {
			LOGGER.error("get evaluList list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
		return "riskEvaluation/addPaperTest";
	}
	
	/**
	 * 获取当前试卷是否被使用
	 */
	@ResponseBody
	@RequestMapping("/isPaperUsed")
	public JsonBean isPaperUsed(@RequestBody Answer model) {
		JsonBean bean = new JsonBean();
		try {
			if(StringUtils.isBlank(model.getPaperId())){
				bean.setCode(0);
				bean.setMessage("参数错误");
				return bean;
			}
			int result = answerServiceImpl.pagedList(model).size();
			bean.setCode(1);
			bean.setData(result);
			bean.setMessage("获取当前试卷是否成功.");
		} catch (Exception e) {
			bean.setCode(0);
			bean.setMessage("获取当前试卷是否失败.");
			LOGGER.error(" get paper isPaperUsed state, errorMsg=[{}].", e.getMessage());
		}
		return bean;
	}
	
	/**
	 * 删除试卷和试卷下的所有试题和试题下的所有选项
	 */
	@ResponseBody
	@RequestMapping("/deletePaper")
	public JsonBean deletePaper(@RequestBody Paper model) {
		JsonBean bean = new JsonBean();
		try {
			if(StringUtils.isBlank(model.getId())){
				bean.setCode(0);
				bean.setMessage("参数错误");
				return bean;
			}
			paperServiceImpl.deleteById(model.getId());
			Question question = new Question();
			question.setPaperId(model.getId());
			question.setTag(false);
			List<Question> quesList = questionServiceImpl.queryList(question);
			//删除所有试题
			questionServiceImpl.deleteBulks(question);
			//删除试题下面的所有选项
			Options options = new Options();
			for (Question que : quesList) {
				options.setQuestionId(que.getId());
				optionsServiceImpl.deleteBulks(options);
			}
			bean.setCode(1);
			bean.setMessage("删除试卷成功.");
		} catch (Exception e) {
			e.printStackTrace();
			bean.setCode(0);
			bean.setMessage("删除试卷失败.");
			LOGGER.error(" get paper isPaperUsed state, errorMsg=[{}].", e.getMessage());
		}
		return bean;
	}
}
